import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home'
import Cart from '../views/Cart'
import Cate from '../views/Cate'
import User from '../views/User'
import Index from '../views/Index'
import Search from '../views/Search'
import Detail from '../views/Detail'
import Login from '../views/Login'
import SelectLogin from '../views/SelectLogin'
import MyOrder from '../views/MyOrder'
import ConfirmOrder from '../views/ConfirmOrder'
import Address from '../views/Address'
import AddAddress from '../views/AddAddress'
import OrderDetails from '../views/OrderDetails'
import UserCenter from '../views/UserCenter'


// AddAddress
Vue.use(VueRouter)

const routes = [
    { path:'/',redirect:'/home'},
    {
      path:'/home',
      component: Home,
      children: [
        { path:'',component: Index,meta: { requiresAuth: false } },
        { path:'cate',component: Cate,meta: { requiresAuth: false } },
        { path:'cart',component: Cart,meta: { requiresAuth: true } },
        { path:'user',component: User,meta: { requiresAuth: true }},
        { path:'detail/:id',component:Detail,meta: { requiresAuth: false }},
      ]
    },
    {
      path:'/selectlogin',
      component:SelectLogin,
      meta: { requiresAuth: false },
    },
    {
      path:'/confirmorder',
      component: () => import('@/views/ConfirmOrder'),
      meta: { requiresAuth: true } ,
    },
    {
      path:'/confirmorder/address',
      component: () => import('@/views/Address'),
      meta: { requiresAuth: true } ,
    },
    {
      path:'/confirmorder/address/addAddress',
      component: () => import('@/views/AddAddress'),
      meta: { requiresAuth: true } ,
    },
    {
      path:'/myorder',
      component: MyOrder ,
    },
    {
      path:'/confirmorder/orderdetails',
      component: () => import('@/views/OrderDetails')
    },
    { path:'/search',component:Search,meta: { requiresAuth: false }},
    
    { path:'/login',component:Login,meta: { requiresAuth: false }},
    { path:'/home/user/usercenter',component:() => import('@/views/UserCenter'),meta: { requiresAuth: true }},
]

const router = new VueRouter({
  routes
})
// 前置守卫
router.beforeEach((to,from,next) => {
  // 用户是否需要登录 没有在本地获取到用户信息就需要登录
  if(to.meta.requiresAuth && !window.localStorage.getItem('userinfo')){
    // 跳转到选择登录页面
    next({
            path:'/selectlogin',
            query:{
              src_url:to.fullPath
            }

        })
  }
  next()
})

export default router
